package classification

import (
	"myserver/app/types"

	pkgApp "myserver/pkg/app"
	"myserver/pkg/constant"

	"myserver/app/service/classification_service"

	"net/http"

	"github.com/gin-gonic/gin"
)

type ClassificationController struct{}

func (ClassificationController) Ceate(c *gin.Context) {
	var (
		m   types.TableClassification
		app = pkgApp.Gin{C: c}
	)
	httpCode, _, err := pkgApp.BindAndValid(c, &m)
	if err != nil {
		app.Response(httpCode, err.Error(), nil)
		return
	}
	server := classification_service.ClassificationService{Data: &m}
	err = server.Ceate()
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, "成功")
}

func (ClassificationController) UpData(c *gin.Context) {
	var (
		ms  []types.TableClassification
		app = pkgApp.Gin{C: c}
	)
	err := c.ShouldBindJSON(&ms)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	if len(ms) == 0 {
		app.Response(http.StatusInternalServerError, "请传入数据", nil)
		return
	}
	server := classification_service.ClassificationService{}
	err = server.UpData(ms)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, "成功")
}

func (ClassificationController) Dell(c *gin.Context) {
	var (
		m   types.DellStruct
		app = pkgApp.Gin{C: c}
	)
	httpCode, errCode, err := pkgApp.BindAndValid(c, &m)
	if err != nil {
		app.Response(httpCode, errCode, err.Error())
		return
	}
	server := classification_service.ClassificationService{}
	err = server.Dell(m.UuIds)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, "操作成功")
}

func (ClassificationController) List(c *gin.Context) {
	var (
		m   types.ClassificationShop
		app = pkgApp.Gin{C: c}
	)

	httpCode, errCode, err := pkgApp.BindAndValid(c, &m)
	if err != nil {
		app.Response(httpCode, errCode, err.Error())
		return
	}
	server := classification_service.ClassificationService{}
	ls, err := server.TreeList(m.ShopUuid)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, ls)
}
